package com.mayi.zds.service.imp;

import com.mayi.zds.dao.BusinessDAO;
import com.mayi.zds.entity.Business;
import com.mayi.zds.exception.BusinessAccountExistException;
import com.mayi.zds.exception.BusinessAccountPasswordErrorException;
import com.mayi.zds.exception.BusinessStatusErrorException;
import com.mayi.zds.service.BusinessService;
import com.mayi.zds.utils.ZdsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
@Transactional
public class BusinessServiceImpl implements BusinessService {
    @Resource
    private BusinessDAO businessDAO;
    @Override
    public void add(Business business) {
        if(businessDAO.getCountByAccount(business.getAccount())!=0){
            throw new BusinessAccountExistException("该商户名已被注册！");
        }
     business.setId(ZdsUtils.produceId());
        business.setIsOpen("y");//默认新增商户为开门状态
        business.setScore(0.00);//初始化评分为0
        business.setAvatar("默认头像.jpg");
        business.setStatus("1");//新增商户默认为待审核状态
        business.setDeliveryArea(0.00);//默认配送距离
        business.setMinimumFee(new BigDecimal("0"));//初始化最低配送费
        business.setIncome(new BigDecimal("0"));//初始化收入
        businessDAO.add(business);
    }

    @Override
    public Business login(String account, String Password) {
        if(businessDAO.getBusinessByAccountAndPassword(account,Password)==null){
            throw new BusinessAccountPasswordErrorException("商户用户名和密码不匹配");
        }
        if(!"1".equals(businessDAO.getBusinessByAccountAndPassword(account,Password).getStatus())){
            throw  new BusinessStatusErrorException("该商户未通过审核或状态异常 请联系管理员");
        }
        return businessDAO.getBusinessByAccountAndPassword(account,Password);
    }
}
